<html>
<head>
<style type="text/css">
UL {
    list-style-type: none;
}
P { 
    margin: 10px;
}
</style>
</head>
<body vlink=blue>
<?
require_once("class_PgMonSQL.php");
require_once("classes/class_Face.php");
require_once("classes/class_WUHostCluster.php");
require_once("classes/class_WUDatabaseName.php");
require_once("classes/class_WUSchemaName.php");

$face=new Face();
if (empty($_REQUEST)) {
    $face->flush_level();
    $face->draw();
    exit;
}

if (isset($_REQUEST['action']) && $_REQUEST['action']=='cluster_add') {
    $face->flush_level();
    $face->set_body(WUHostCluster::cluster_add_edit_form());
    $face->draw();
    exit;
}

$face->set_level($_REQUEST['level'],$_REQUEST['id']);

if ($_REQUEST['level']=='cluster') {
    $hc=new WUHostCluster($_REQUEST['id']);
    if (isset($_REQUEST['action'])) {
	if ($_REQUEST['action']=='view') {
	    $face->set_body($hc->get_view());
	}
	if ($_REQUEST['action']=='edit') {
	    $face->set_body($hc->cluster_add_edit_form($hc->get_id()));
	}
	if ($_REQUEST['action']=='discover') {
	    $hc->discover();
	    $face->set_body($hc->get_view());
	}
    }
    if (isset($_POST['edit_cluster_submit'])) {
	$hc->update_cluster($_POST);
	$face->set_body($hc->get_view());
    }
}

if ($_REQUEST['level']=='database') {
    $dn=new WUDatabaseName($_REQUEST['id']);
    if (isset($_REQUEST['action'])) {
	if ($_REQUEST['action']=='view') {
	    $face->set_body($dn->get_view());
	}
	if ($_REQUEST['action']=='discover') {
	    $dn->discover();
	    $face->set_body($dn->get_view());
	}
    }
    if (isset($_POST['edit_dn__submit'])) {
	$dn->update_database($_POST);
	$face->set_body($dn->get_view());
    }
}

if ($_REQUEST['level']=='schema') {
    $sn=new WUSchemaName($_REQUEST['id']);
    if (isset($_REQUEST['action'])) {
	if ($_REQUEST['action']=='view') {
	    $face->set_body($sn->get_view());
	}
//	if ($_REQUEST['action']=='discover') {
//	    $sn->discover();
//	    $face->set_body($sn->get_view());
//	}
    }
    if (isset($_POST['edit_sn__submit'])) {
	$sn->update_schema($_POST);
	$face->set_body($sn->get_view());
    }
}

$face->draw();

function add_cluster() {
    $hc=new HostCluster;

    $hc->set_field('hostname',$_POST['add_cluster_hostname']);
    $hc->set_field('ip_address',$_POST['add_cluster_ip_address']);
    $hc->set_field('is_master',$_POST['add_cluster_is_master']);

    $hc->set_field('alive','t');

    if (isset($_POST['add_cluster_fqdn'])) {
	$hc->set_field('fqdn',$_POST['add_cluster_fqdn']);
    }
    if (isset($_POST['add_cluster_spec_comments'])) {
	$hc->set_field('spec_comments',$_POST['add_cluster_spec_comments']);
    }
    /*****************/
    $conn_param="{";
    $conn_param.="hostaddr=".$_POST['add_cluster_ip_address'].",";

    $conn_param.="dbname=".($_POST['add_cluster_conn_dbname'] ? $_POST['add_cluster_conn_dbname'] : 'postgres').",";
    $conn_param.="port=".($_POST['add_cluster_conn_port'] ? $_POST['add_cluster_conn_port'] : '5432').",";
    $conn_param.="user=".($_POST['add_cluster_conn_user'] ? $_POST['add_cluster_conn_user'] : 'postgres').",";
    if ($_POST['add_cluster_conn_password']) {
	$conn_param.="password=".$_POST['add_cluster_conn_password'].",";
    }

    $conn_param.="sslmode=".$_POST['add_cluster_conn_sslmode']."}";
    $hc->set_field('conn_param',$conn_param);

    $hc->save();
}



?>
</body>
</html>
